package Pack;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Fenetre extends JFrame
{
	public static final int N_PARMS = 4;


	MonFiltre filtreJpg = new MonFiltre(new String[]{"jpeg","jpg"},"les fichiers image (*.jpeg, *.jpg)");
	MonFiltre filtreMp3 = new MonFiltre(new String[]{"mp3"},"les fichiers son (*.mp3)");
	MonFiltre filtreMpeg = new MonFiltre(new String[]{"mpeg"},"les fichiers video (*.mpeg)");

	public List<String> listeImages = new ArrayList<String>();
	public String cheminI="/";
	public String cheminS="/";
	public String cheminSave="/";
	public int largeur=400, hauteur=300;
	
	private JFrame thisFrame;

	private JMenuBar menuBar;
	private JMenu menuAction;
	private JMenuItem menuAdd;
	private JMenuItem menuAddS;
	private JMenuItem menuGo;
	private JMenuItem menuItemDim;
	private JMenuItem menuItemSave;
	private JMenuItem menuItemRun;
	private JMenuItem menuItemResume;
	private JMenuItem menuItemQuit;
	private JMenu menuHelp;
	private JMenuItem menuItemAbout;
	public JProgressBar progressBar;



	public Fenetre(String title) {
		thisFrame = this;

		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch (InstantiationException e) {}
		catch (ClassNotFoundException e) {}
		catch (UnsupportedLookAndFeelException e) {}
		catch (IllegalAccessException e) {}

		this.setTitle(title);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		menuBar = new JMenuBar();
		menuAction = new JMenu("Action");
		menuBar.add(menuAction);
		menuItemDim = new JMenuItem("Dimensions video");
		menuItemDim.addActionListener(new DimMenuListener());
		menuAction.add(menuItemDim);
		menuAdd = new JMenuItem("Ajouter une image");
		menuAdd.addActionListener(new AddMenuListener());
		menuAction.add(menuAdd);
		menuAddS = new JMenuItem("Ajouter une musique");
		menuAddS.addActionListener(new AddSoundMenuListener());
		menuAction.add(menuAddS);
		menuGo = new JMenuItem("GO !");
		menuGo.addActionListener(new GoMenuListener());
		menuAction.add(menuGo);
		menuItemQuit = new JMenuItem("Quitter");
		menuItemQuit.addActionListener(new QuitMenuListener());
		menuAction.add(menuItemQuit);
		menuHelp = new JMenu("Aide");
		menuBar.add(menuHelp);
		menuItemAbout = new JMenuItem("A propos..");
		menuItemAbout.addActionListener(new AboutMenuListener());
		menuHelp.add(menuItemAbout);
		this.setJMenuBar(menuBar);
		menuItemResume = new JMenuItem("Resume");
		menuItemResume.addActionListener(new ResumeMenuListener());
		menuAction.add(menuItemResume);

      /*
      menuItemSave = new JMenuItem("Fichier Dest .avi");
      menuItemSave.addActionListener(new SaveMenuListener());
      menuAction.add(menuItemSave);
      menuItemRun = new JMenuItem("Lancer");
      menuItemRun.addActionListener(new RunMenuListener());
      menuAction.add(menuItemRun);*/

		progressBar = new JProgressBar(0, 100);
		this.getContentPane().setLayout(new FlowLayout());
		this.getContentPane().add(progressBar);
		this.pack();
		this.setBounds(200, 200, 200, 80);
		this.setResizable(true);
		this.setVisible(true);
	}
	
	class AddMenuListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			fileChooserParameter();
		}
	}
	class AddSoundMenuListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			fileChooserParameter2();
		}
	}

	class GoMenuListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if(fileChooserParameter3())
				EssaiClass.go(largeur, hauteur, listeImages, cheminS, cheminSave, progressBar);
		}
	}

	class DimMenuListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			largeur= aToi(JOptionPane.showInputDialog(thisFrame,"Entrez la largeur", 
					"Largeur",JOptionPane.PLAIN_MESSAGE));
			hauteur= aToi(JOptionPane.showInputDialog(thisFrame,"Entrez la hauteur", 
					"Hauteur",JOptionPane.PLAIN_MESSAGE));
			if(largeur==0 && hauteur==0){
				largeur=400;
				hauteur=300;
			}
			else if(largeur==0 && hauteur!=0){
				largeur=(4*hauteur)/3;
			}
			else if(largeur!=0 && hauteur==0){
				hauteur= (int)(3*largeur)/4;
			}
			System.out.println(largeur+"x"+hauteur);
			
		}
	}

	class SaveMenuListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			fileChooserParameter();
		}
	}
	/*
	class RunMenuListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if (validFrameRate(parameter[FRAME_RATE_TEXT])) {
				//file.saveParameters("bmpseq.ini", parameter, parameterType);
				Graphics graphics = thisFrame.getGraphics();
				thisFrame.paint(graphics);
				int ret = bmpSeq.frontEnd(
          parameter[START_FILE_NAME],
	  parameter[END_FILE_NAME],
	  aToi(parameter[FRAME_RATE_TEXT]),
	  parameter[SAVE_FILE_NAME],
	  thisFrame, progressBar);
				Toolkit.getDefaultToolkit().beep();
				//if (ret == 0)
				//file.alert(thisFrame, "Fini");
				//else
				//file.alert(thisFrame, "Echec");
				progressBar.setValue(0); // reset
			}
		}
	}*/

	class ResumeMenuListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			JOptionPane.showConfirmDialog(thisFrame,
					"Dimensions video :   " + largeur + "x" + hauteur +"\n" +
					"Musique                 :   " + cheminS + "\n" +
					"Liste des images    :\n" + afficheListe(listeImages)+"\n" ,
					"Parametre",
					JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE);
		}
	}

	class QuitMenuListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			System.exit(0);
		}
	}

	class AboutMenuListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			JOptionPane.showConfirmDialog(thisFrame,
					"Version 1.04\n" +
					"Auteur IUT Orleans Dep. Info\n" +
					"tony.galbert@etu.univ-orleans.fr",
					"A propos..",
					JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE);
		}
	}

	private String fileChooser() {
		JFileChooser fileChooser = new JFileChooser("Ajouter une image");
		fileChooser.setCurrentDirectory(new File(cheminI));
		
		fileChooser.addChoosableFileFilter(filtreJpg);
		int returnVal = fileChooser.showOpenDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			cheminI=fileChooser.getSelectedFile().getPath();
			return fileChooser.getSelectedFile().getPath();
		}
		return "";
	}
	private String fileChooser2() {
		JFileChooser fileChooser = new JFileChooser("Ajouter une musique");
		fileChooser.setCurrentDirectory(new File(cheminS));
		
		fileChooser.addChoosableFileFilter(filtreMp3);
		int returnVal = fileChooser.showOpenDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			String url=fileChooser.getSelectedFile().getPath();
			return url;
		}
		return "";
	}
	private String fileChooser3() {
		JFileChooser fileChooser = new JFileChooser("Enregistrer la video");
		fileChooser.setCurrentDirectory(new File(cheminSave));
		
		fileChooser.addChoosableFileFilter(filtreMpeg);
		int returnVal = fileChooser.showSaveDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			String url=fileChooser.getSelectedFile().getPath();
			return url;
		}
		return "";
	}

	private void fileChooserParameter() {
		String fileName = fileChooser();
		if (!fileName.equals("")) // found a file
			listeImages.add(fileName);
	}
	private void fileChooserParameter2() {
		String fileName = fileChooser2();
		if (!fileName.equals("")) // found a file
			cheminS="\""+fileName+"\"";
		
	}
	private boolean fileChooserParameter3() {
		String fileName = fileChooser3();
		if (!fileName.equals("")){ // found a file
			cheminSave= auFormatMpeg(fileName);
			return true;
		}
		return false;
	}

	public String afficheListe (List<String> liste){
		String res = "";
		for(int i=0; i<liste.size() ; i++){
			res+=(" "+(i+1)+" = "+liste.get(i)+"\n");
		}
		return res;
	}
	private int aToi(String string) {
		int i;
		try {
			i = (new Integer(string)).intValue();
		}
		catch (NumberFormatException e) {
			i = 0;
		}
		return i;
	}

	public boolean isNumber(String string) {
		int length = string.length();
		for (int i = 0; i < length; i++)
			if (!Character.isDigit(string.charAt(i)))
				return false;
		return true;
	}

	public boolean validFrameRate(String frameRate) {
		if (!isNumber(frameRate)) {
			//file.alert(this, "La frequence doit etre un nombre");
			return false;
		}
		int n = aToi(frameRate);
		if (n < 1 || n > 30) {
			//file.alert(this, "La frequence doit etre comprise entre 1 et 30");
			return false;
		}
		return true;
	}

	public String auFormatMpeg(String original){
		if(original.endsWith(".mpeg"))
			return original;
		else 
			return original+".mpeg";
	}
}